package com.etc.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Article;
import com.etc.entity.Comments;
import com.etc.entity.User;
import com.etc.service.ArticleService;
import com.etc.service.CommentsService;

@Controller
@RequestMapping("essay")
public class EssayController {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private CommentsService commentsService;
	
	//支持按钮加分
	private final static int SUPPORTSOCRE=3;
	//判断是否支持过该文章
	private static Boolean SUPPORTFLAG=false;
	//判断是否不喜欢该文章
	private final static int OPPOSESCORE=2;
	
	@RequestMapping("deleteComment")
	@ResponseBody
	public Map<String ,Object> deleteComment(Integer commentcid){
		Map<String, Object> map=new HashMap<String, Object>();
		Boolean b = commentsService.deleteCommentBycid(commentcid);
		if (b) {
			map.put("ret",0);
		}else{
			map.put("msg", "删除出问题了哦！");
		}
		
		return map;
	}
	
	@RequestMapping("submitsubcomment")
	@ResponseBody
	public Map<String ,Object> submitSubComment(Comments comment,HttpSession session){
		Map<String, Object> map=new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		if (user==null) {
			//用户未登录
			map.put("ret", -1);
			map.put("msg","您尚未登录，请登录");
			return map;
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
		comment.setCcreatetime(sdf.format(new Date()));
		comment.setUser(user);
		System.out.println(comment);
		System.out.println(comment.getUser());
		int i = commentsService.insertComments(comment);
		if (i>0) {
			map.put("ret", 0);
			map.put("addComment", comment);
		}else{
			map.put("ret",-2);
			map.put("msg","啊哦，出错了");
		}
		return map;
	}
	
	
	@RequestMapping("commentadd")
	@ResponseBody
	public Map<String, Object> commentAdd(Comments comment,HttpSession session){
		Map<String, Object> map=new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		if (user==null) {
			//用户未登录
			map.put("ret", -1);
			map.put("msg","您尚未登录，请登录");
			return map;
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
		comment.setCcreatetime(sdf.format(new Date()));
		comment.setUser(user);
		int i = commentsService.insertComments(comment);
		if (i>0) {
			int j = commentsService.updateCommentBycid(comment);
			if (j>0) {
				map.put("ret", 0);
			}else{
				map.put("ret",-2);
			}
		}else{
			map.put("ret", -2);
		}
		return map;
	}
	
	@RequestMapping("Vote")
	@ResponseBody
	public Map<String, Object> vote(@RequestParam("id")Integer aid,@RequestParam("act")String act){
		Map<String, Object> map=new HashMap<String, Object>();
		//如果没有点击过支持按钮
		if(SUPPORTFLAG==false){
			Article article = articleService.selectArticleByAid(aid);
			System.out.println(article);
			if ("support".equals(act)) {
				//文章得分+3分
				article.setAscore(article.getAscore()+SUPPORTSOCRE);
				int i = articleService.updateArticle(article);
				if (i>0) {
					SUPPORTFLAG=true;
					map.put("ret", 0);//返回0代表一切正常
				}else{
					map.put("ret", -1000);
					
					map.put("msg","啊哦，出错了！");//返回出错信息
				}
			}
			if ("oppose".equals(act)) {
				//文章得分+3分
				article.setAscore(article.getAscore()-OPPOSESCORE);
				int i = articleService.updateArticle(article);
				if (i>0) {
					SUPPORTFLAG=true;
					map.put("ret", 0);//返回0代表一切正常
				}else{
					map.put("ret", -1000);
					
					map.put("msg","啊哦，出错了！");//返回出错信息
				}
			}
		}else{
			map.put("ret", 34);//表示已经评论过该文章了 
		}
		return map;
	}
	
	
	
}
